package com.yutianran.springBoot.thread;

import java.util.concurrent.*;

public class ThreadHelper {

    public static final ExecutorService getInstance() {
        return ThreadHelperHolder.executorService;
    }

    public static class ThreadHelperHolder {
        private static final RejectedExecutionHandler defaultHandler = new ThreadPoolExecutor.DiscardOldestPolicy();
        private static final int NUMBER_OF_CORES = Runtime.getRuntime().availableProcessors();
        private static final int KEEP_ALIVE_TIME = 1;
        private static final TimeUnit KEEP_ALIVE_TIME_UNIT = TimeUnit.SECONDS;
        private static final BlockingQueue taskQueue = new LinkedBlockingQueue(128);
        private static final ExecutorService executorService = new ThreadPoolExecutor(NUMBER_OF_CORES, NUMBER_OF_CORES * 2, KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT, taskQueue, new BackgroundThreadFactory(), new DefaultRejectedExecutionHandler());
    }

}
